#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <stdbool.h>

#define N 10

int sum(int *arr, int n);
int* getPrimes(int n);
bool isPrime(int x);

// Usage: a.out [n]
// 如果用户有传入第二个命令行参数，则生成用户指定个数的素数
int main(int argc, char **argv) {

    int n = N;         //default to the first 10 primes
    if(argc == 2) {
        n =  atoi(argv[1]);
    }

    int *primes = getPrimes(n);

    int s = sum(primes, n);
    printf("The sum of the first %d primes is %d\n", n, s);

    return 0;
}

int sum(int *arr, int n) 
{
    int i = 0;
    int total = 0;
    for(; i<n; i++) 
    {
        total += arr[i];
    }
    return total;
}

int* getPrimes(int n)
{
    int* result = (int*)malloc(n * sizeof(int));
    if(result == NULL)
    {
        fprintf(stderr, "Error: malloc failed\n");
        exit(1);
    }

    int i = 0;
    int x = 2;
    while(i < n) 
    {
        if(isPrime(x)) 
        {
            result[i] = x;
            i++;
        }
        x++;
    }
    return result;
}

bool isPrime(int x) 
{
    if(x == 2)
        return true;
    if(x % 2 == 0) 
        return false;

    for(int i = 2; i <= sqrt(x); i++) 
    {
        if(x % i == 0) 
        {
            return false;
        }
    }
    return true;
}
